package com.soecode.lyf.config;

import com.soecode.lyf.service.BookService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class LookupType implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    private static BookService bookService;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(LookupType.applicationContext ==null){
            synchronized (LookupType.class){
                LookupType.applicationContext = applicationContext;
            }
        }
    }

    /**
     * 这里不使用双检锁的原因是，这里的bookService是单例的，所以不需要双检锁
     *
     * @return
     */
    public BookService getInstance() {
        if (bookService == null) {
            synchronized (LookupType.class) {
                bookService = applicationContext.getBean(BookService.class);
            }
        }
        return bookService;
    }
}
